package com.paul.sys.service.impl;


import com.paul.sys.mapper.SymenuMapper;
import com.paul.sys.mapper.SyroleMapper;
import com.paul.sys.model.Ptstaff;
import com.paul.sys.model.Syrole;
import com.paul.sys.service.SyroleService;
import com.paul.sys.tools.CommPageBean;
import com.paul.sys.tools.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yingg_000 on 2015/7/21.
 */
@Service
public class SyroleServiceImpl implements SyroleService {
    @Autowired
    SyroleMapper syroleMapper;
    @Autowired
    SymenuMapper symenuMapper;

    @Override
    public int delete(String unid) {
        return syroleMapper.delete(unid);
    }

    @Override
    public int deletes(String[] unids) {
        int result = 0;
        for (int i = 0; i < unids.length; i++) {
            result = result + syroleMapper.delete(unids[i]);
        }
        return result;
    }

    @Override
    public int insert(Syrole record) {
        return syroleMapper.insert(record);
    }

    @Override
    public Syrole selectById(String unid) {
        return syroleMapper.selectById(unid);
    }

    @Override
    public int update(Syrole record) {
        return syroleMapper.update(record);
    }

    @Override
    public List<Map> roleNamejudge(String rolename) {
        return syroleMapper.roleNamejudge(rolename);
    }

    @Override
    public List<Syrole> listRole(Map<String, String> param) {
        return syroleMapper.listRole(param);
    }

    @Override
    public PageData<Syrole> listByPage(Map<String, String> param) {
        PageData<Syrole> pageData = new PageData<Syrole>();
        CommPageBean pageBean = CommPageBean.getPageBeanByParam(param);
        List<Syrole> list = syroleMapper.listByPage(param, pageBean);
        pageData.setPageBean(pageBean);
        pageData.setListData(list);
        return pageData;
    }

    @Override
    public List<Map> getTreebyRole(String unid) {
        return syroleMapper.getTreebyRole(unid);
    }

    @Override
    public PageData<Ptstaff> getUserbyRole(String roleunid, Map<String, String> param) {
        PageData<Ptstaff> pageData = new PageData<Ptstaff>();
        CommPageBean pageBean = CommPageBean.getPageBeanByParam(param);
        List<Ptstaff> list = syroleMapper.getUserbyRoleByPage(roleunid, pageBean);
        pageData.setPageBean(pageBean);
        pageData.setListData(list);
        return pageData;
    }

    @Override
    @Transactional
    public int linkMenu(Map<String, Object> param) {
        param.replace("enable", "0");
        syroleMapper.menuLinkEnableByRole(param);//先将此角色下的关系目录全设置为不可用
        param.replace("enable", "1");
        String[] arr = param.get("menuunids").toString().split(",");
        Map<String, Object> parm2 = null;
        for (int i = 0; i < arr.length; i++) {
            parm2 = new HashMap<String, Object>();
            parm2.putAll(param);
            parm2.put("menuunid", arr[i].toString());
            List list = symenuMapper.selectRoleLink(parm2);
            if (list.size() == 1) {
                symenuMapper.updateRoleLinkEnable(parm2);
            } else {
                symenuMapper.insertRoleLink(parm2);
            }
        }
        return 1;
    }

    @Override
    @Transactional
    public int unlinkMenu(Map<String, Object> param) {
        param.replace("enable", "1");
        syroleMapper.menuLinkEnableByRole(param);
        param.replace("enable", "0");
        String[] arr = param.get("menuunids").toString().split(",");
        Map<String, Object> parm2 = null;
        for (int i = 0; i < arr.length; i++) {
            parm2 = new HashMap<String, Object>();
            parm2.putAll(param);
            parm2.put("menuunid", arr[i].toString());
            List list = symenuMapper.selectRoleLink(parm2);
            if (list.size() == 1) {
                symenuMapper.updateRoleLinkEnable(parm2);
            } else {
                symenuMapper.insertRoleLink(parm2);
            }
        }
        return 1;
    }

    @Override
    public List<Map> getSimulationMessage() {
        return syroleMapper.getSimulationMessage();
    }
}
